package com.ptmall.app.ui.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.ShareCouponBean;
import com.ptmall.app.bean.SpreadBean;
import com.ptmall.app.bean.model.db.Account;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.ShareAdapter;
import com.ptmall.app.ui.adapter.ShareCouponAdapter;
import com.ptmall.app.view.NoScrollListView;
import com.ptmall.app.view.TitleBarView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShareCouponActivity extends BaseMvpActivity {

    RecyclerView couponlist;

    ShareCouponAdapter mShareCouponAdapter;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    TextView yq_num,quan_num,yqm,fz;

    Button fenxiang;

    TitleBarView titlebar;

    ShareAdapter shareAdapter;
    NoScrollListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_sharecoupon);
    }

    @Override
    public void initViews() {
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        couponlist = (RecyclerView) findViewById(R.id.couponlist);
        yq_num = (TextView) findViewById(R.id.yq_num);
        quan_num = (TextView) findViewById(R.id.quan_num);
        yqm = (TextView) findViewById(R.id.yqm);
        fenxiang = (Button) findViewById(R.id.fenxiang);
        listview = (NoScrollListView) findViewById(R.id.listview);
    }

    @Override
    public void initEvents() {
        mShareCouponAdapter =new ShareCouponAdapter(getActivity());
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getContext());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        couponlist.setLayoutManager(mLinearLayoutManager);
        couponlist.setAdapter(mShareCouponAdapter);
        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        fenxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                apiDataRepository.getUserInfo(null, new ApiNetResponse<Account>(getContext()) {
                    @Override
                    public void onSuccess(Account data) {
                        Intent textIntent = new Intent(Intent.ACTION_SEND);
                        textIntent.setType("text/plain");
                        textIntent.putExtra(Intent.EXTRA_TEXT, data.shareurl);
                        startActivity(Intent.createChooser(textIntent, "分享"));
                    }
                });
            }
        });
        getQuan();
        getData();
    }

    private void getQuan(){
        apiDataRepository.getQuan(null, new ApiNetResponse<List<ShareCouponBean>>() {
            @Override
            public void onSuccess(List<ShareCouponBean> data) {
                mShareCouponAdapter.bind(data);
                mShareCouponAdapter.setClickInterface(new ShareCouponAdapter.ClickInterface() {
                    @Override
                    public void addq(int position, String id) {
                        Map<String,String> map = new HashMap<>();
                        map.put("quanId",id);
                        ApiDataRepository.getInstance().lq(map, new ApiNetResponse<Object>() {
                            @Override
                            public void onSuccess(Object data) {
                                showMsg("领取成功");
                            }
                        });
                    }
                });
            }
        });
    }

    private void getData(){
        apiDataRepository.getSpread(null, new ApiNetResponse<SpreadBean>() {
            @Override
            public void onSuccess(SpreadBean data) {
                yq_num.setText(data.count);
                quan_num.setText(data.quan);
                shareAdapter = new ShareAdapter();
                listview.setAdapter(shareAdapter);
                shareAdapter.pushData(data.list);
            }
        });
        apiDataRepository.getUserInfo(null, new ApiNetResponse<Account>() {
            @Override
            public void onSuccess(Account data) {
                yqm.setText(data.memberId+"");
                fz.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 从API11开始android推荐使用android.content.ClipboardManager
                        // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        // 将文本内容放到系统剪贴板里。
                        cm.setText(data.memberId+"");
                        showMsg("复制成功");
                    }
                });
            }
        });
    }
}
